<template>
  <div class="dashboard-container">
    <div class="app-container">

      <el-card>
        <el-tabs>
          <el-tab-pane label="登录账户设置">

            <!-- 放置表单 -->
            <el-form ref="userForm" :model="userInfo" :rules="rules" label-width="120px" style="margin-left: 120px; margin-top:30px">
              <el-form-item label="姓名:" prop="username">
                <el-input v-model="userInfo.username" style="width:300px" />
              </el-form-item>
              <el-form-item label="密码:" prop="password2">
                <el-input v-model="userInfo.password2" style="width:300px" type="password" />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="btnOK">更新</el-button>
              </el-form-item>
            </el-form>
          </el-tab-pane>
          <el-tab-pane label="个人详情">
            <!-- <UserInfo />
            除了用引入注册的方式渲染组件, 还可以用动态组件的形式实现
            固定使用 component 标签名, 传入一个 is字符串指定当前组件显示的名称即可 -->
            <component :is="userComponent" />
          </el-tab-pane>
          <el-tab-pane label="岗位信息">
            <component :is="jobComponent" />
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
  </div>
</template>

<script>
import { getUserDetailById } from '@/api/user'
import { saveUserDetailById } from '@/api/employees'
import UserInfo from './components/user-info'
import JobInfo from './components/job-info'
export default {
  components: {
    UserInfo,
    JobInfo
  },
  data() {
    return {
      userId: this.$route.params.id,
      userComponent: 'UserInfo',
      jobComponent: 'JobInfo',
      userInfo: {
        username: ''
      },
      rules: {
        username: [
          { required: true, message: '请填写信息', trigger: 'blur' }
        ],
        password2: [
          { required: true, message: '请填写信息', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getUserDetailById()
  },
  methods: {
    async getUserDetailById() {
      this.userInfo = await getUserDetailById(this.userId)
    },
    async btnOK() {
      try {
        await this.$refs.userForm.validate()
        // 1. 页面上使用 password2 管理密码
        // 2.后端只认 password
        // 所以发送前需要将 password2 赋值给 password
        // console.log(this.userInfo)
        // console.log({ ...this.userInfo, password: this.userInfo.password2 })
        await saveUserDetailById({ ...this.userInfo, password: this.userInfo.password2 })

        this.$message.success('操作成功')
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>

<style>

</style>
